﻿from numpy import *
import matplotlib
import matplotlib.pyplot as plt
import functools
import os

def load_table_file(filename, sep=None):
    '''加载以表格形式组织的数据
    '''
    table = []
    for line in open(filename):  
        table.append(line.strip().split(sep))
    return table

def load_sample_file(filename, sep=None):
    table = load_table_file(filename, sep)

    return [i[0:-1] for i in table], [i[-1] for i in table]
        
'''    
def loadSampleFile(filename, sep='\t'):
    """加载样本数据文件

    Parameters
    ----------
    filename: 
        数据文件路径，以`sep`分割的列表，最后一列为类型标识
    sep:
        数据分隔符

    Returns
    -------
    data: matrix
        文件中出去最后一列的数据
    labels: list[string]
        文件中最后一列的类型标识
    """
        
    fr = open(filename)
    lines = fr.readlines()
    numberOfLines = len(lines)
    assert(numberOfLines > 0)
    numberOfColumn = len(lines[0].strip().split(sep))
    assert(numberOfColumn >= 2)
    data = zeros((numberOfLines, numberOfColumn-1))
    labels = []
    index = 0
    for line in lines:
        line = line.strip()
        items = line.strip().split(sep)
        data[index,:] = items[0:numberOfColumn-1]
        labels.append(items[-1])
        index += 1
    return data, labels

'''
def getTestSample(name):
    if name == "ex0":
        return loadSampleFile(os.path.join(os.path.dirname(__file__), 'res', 'ex0.txt'))
    return None



def loadBitTextImage(path):
    '''加载01构成的位图
    '''




def loadHandWritingDigit(path):
    """加载手写的数字图片，文件有32*32个01组成的的文本，文件名x_nn.txt的x对应数字

    Parameters
    ----------
    path:
        文件路径

    Returns
    -------
    data: 
        1*1024的数组
    label:
        对应的实际数字
    """

    import os
    label = os.path.split(path)[1].split('_')[0]

    data = zeros((1, 1024))
    with open(path) as f:
        for i in range(32):
            line = f.readline()
            for j in range(32):
                data[0, 32*i+j] = int(line[j])
    return data, label


def storeObject(obj, path):
    with open(path, 'w') as fw:
        import pickle
        fw = open(path,'w')
        pickle.dump(obj,fw)
    
def grabObject(path):
    with open(path) as fr:
        import pickle
        return pickle.load(fr)


def splitEnglishDocument(doc):
    import re
    tokens = re.split(r'\W*', doc)
    return [t.lower() for t in tokens if len(t) > 2]

